package mindshare;

import info.androidhive.slidingmenu.Answer;
import info.androidhive.slidingmenu.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import data_retrieval.GetPicture;
import data_retrieval.ManageFriendship;
import fragments.Post;

public class HomeListAdapter extends ArrayAdapter<Post> {
	Context mc;
	private int type;
	private String name;
	private List<Post> list;
	MenuItem m;
	SessionManager session;
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	List<NameValuePair> picParam = new ArrayList<NameValuePair>();

	public HomeListAdapter(Context c, int type, List<Post> list, MenuItem m) {
		super(c, type, list);
		this.mc = c;
		this.m = m;
		this.list = list;
		this.type = type;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View itemView = convertView;
		if (itemView == null) {
			itemView = LayoutInflater.from(mc).inflate(type, parent, false);
		}
		TextView tvUserName = (TextView) itemView.findViewById(R.id.tvUsername);
		tvUserName.setText(list.get(position).getName());

//		TextView tvTime = (TextView) itemView.findViewById(R.id.tvTime);
//		tvTime.setText(list.get(position).getTime());

		TextView tvPost = (TextView) itemView.findViewById(R.id.tvPost);
		tvPost.setText(list.get(position).getText());
		session = new SessionManager(mc);
		HashMap<String, String> user = session.getUserDetails();
		name = user.get(SessionManager.KEY_NAME);
		
		final ImageView btnReply = (ImageView) itemView.findViewById(R.id.reply_image);
		btnReply.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				btnReply.setAlpha(50);
				int postId = list.get(position).getPostId();
				Intent intention = new Intent(mc, Answer.class);
				intention.putExtra("post_id", postId+"");
				intention.putExtra("question", list.get(position).getText());
				intention.putExtra("name", list.get(position).getName());
				mc.startActivity(intention);
				btnReply.setAlpha(100);
//				params.clear();
//				params.add(new BasicNameValuePair("nickname_2", name));
//				params.add(new BasicNameValuePair("nickname", list.get(position).getNickname()));
//				new ManageFriendship(mc, params, "state", m, "deleteRequest").execute();

			}
		});

		ImageView btnPhoto = (ImageView) itemView.findViewById(R.id.photo_image);
		btnPhoto.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
//				Log.d("ACCEPT", list.get(position).getNickname());
//				params.clear();
//				params.add(new BasicNameValuePair("nickname_2", name));
//				params.add(new BasicNameValuePair("nickname", list
//						.get(position).getNickname()));
//				new ManageFriendship(mc, params, "state", m, "accept")
//						.execute();
			}
		});

		return itemView;
	}
}